﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.Model;
using ZF.BLL;
using GCELibrary.Current;
using ZF.BLL.Linq;
using ZF.DAL;
using System.Web.UI.HtmlControls;

namespace ZF.WS.info
{
    public partial class list : BLL.Page.PageBase
    {
        [PageArgument(Key = "id", DefaultValue = -1)]
        public int Id { get; set; }
        [PageArgument(Key = "pi",DefaultValue=0)]
        public int Pi { get; set; }
        [PageArgument(Key = "days", DefaultValue = 0)]
        public int days { get; set; }
        [PageArgument(Key = "pmin", DefaultValue = 0)]
        public int pmin { get; set; }
        [PageArgument(Key = "pmax", DefaultValue = 0)]
        public int pmax { get; set; }
        [PageArgument(Key = "key", DefaultValue=null)]
        public string key { get; set; }
        [PageArgument(Key = "aid1", DefaultValue = 0)]
        public int AreaId1 { get; set; }
        [PageArgument(Key = "aid2", DefaultValue = 0)]
        public int AreaId2 { get; set; }
        protected string _param;

        protected void Page_Load(object sender, EventArgs e)
        {
            using (var dc = SiteInfo.CreateDBDataContext())
            {
                string param = "?";
                foreach (var name in Request.QueryString.AllKeys)
                {
                    if (name != null && !name.EqIgnoreCase("pi"))
                    {
                        param += "&" + name + "=" + HttpUtility.UrlEncode(Request.QueryString[name]);
                    }
                }

                _param = "/info/list.aspx"+param;


                var sel = dc.I_Info.Where(p => p.Status == 1 && p.IsVerify );

                if (AreaId1 > 0)
                    sel = sel.Where(p => p.AreaId1 == AreaId1);
                if (AreaId2 > 0)
                    sel = sel.Where(p => p.AreaId2 == AreaId2);



                if (Id != -1)
                    sel = sel.Where(p => p.InfoTypeId == this.Id);
                var top = sel.Where(p => p.IsTop);

                if (days > 0)
                    sel = sel.Where(p => p.AddTime > DateTime.Now.AddDays(-days));
                if(pmin>0)
                    sel = sel.Where(p => p.Price >= pmin);
                if (pmax > 0)
                    sel = sel.Where(p => p.Price <= pmax);
                if (!string.IsNullOrEmpty(key))
                    sel = sel.Where(p => p.Title.Contains(key) || p.Tag.Contains(key));

                var pagesize = 15;
                var rowcount = sel.Where(p=>!p.IsTop)
                   .OrderByDescending(p => p.AddTime).Count();
                var pagecount = rowcount / pagesize;
                if (rowcount % pagesize != 0)
                    pagecount++;
                if (Pi >= pagecount)
                    Pi = pagecount-1;




                var tlist = top.OrderByDescending(p => p.InfoId).ToList();

                if (rowcount > 0)
                {
                    tlist.AddRange(sel.Where(p => !p.IsTop)
                        .OrderByDescending(p => p.InfoId).Skip(Pi * pagesize)
                        .Take(pagesize).ToList());
                }
                this.lvList.DataSource = tlist.Select(p => new
                {
                    Info = p,
                    PCtn = HttpUtility.HtmlEncode(p.Content),
                    City = SiteInfo.Instance.GetAreaOrDefault(p.AreaId1).Name,
                    Price = p.Price == 0 ? "面议" : p.Price == -1 ? "--" : p.Price.ToString()+"元",
                }).ToList();
                this.lvList.DataBind();


            }
        }


        protected void InitControls()
        {
        }
    }
}
